#include "stdafx.h"
#include "SQLiteJsCallImp.h"

CSQLiteJsCallImp::CSQLiteJsCallImp()
{
	m_pSQLStatement = NULL;
	m_pRs = NULL;
}
CSQLiteJsCallImp::~CSQLiteJsCallImp()
{
	if(NULL != m_pSQLStatement)
	{
		m_pSQLStatement->Reset();
		m_pSQLStatement.Release();
	}
	if(NULL != m_pRs)
	{
		m_pRs.Release();
	}
}
void CSQLiteJsCallImp::BeginTransaction()
{
	if( NULL != m_pDB && m_pDB->IsOpen())
		m_pDB->BeginTransaction();
}
void CSQLiteJsCallImp::CommitTransaction()
{
	if( NULL != m_pDB && m_pDB->IsOpen())
		m_pDB->CommitTransaction();
}
void CSQLiteJsCallImp::RollbackTransaction()
{
	if( NULL != m_pDB && m_pDB->IsOpen())
		m_pDB->RollbackTransaction();
}
BOOL CSQLiteJsCallImp::ExecuteQuery()
{
	if( NULL != m_pSQLStatement)
	{
		if(NULL != m_pRs)
		{
			m_pRs.Release();
		}
		HRESULT hr = m_pSQLStatement->ExecuteQuery(&m_pRs);
		if(SUCCEEDED(hr))
			return TRUE;

	}
	return FALSE;
}
void CSQLiteJsCallImp::Bind(int idx,int nValue)
{
	if( NULL != m_pSQLStatement)
	{
		m_pSQLStatement->Bind(idx,nValue);
	}
}
void CSQLiteJsCallImp::Bind(int idx, LPCWSTR szValue) 
{
	if( NULL != m_pSQLStatement)
	{
		m_pSQLStatement->Bind(idx,szValue);
	}
}
BOOL CSQLiteJsCallImp::IsEof() 
{
	if( NULL != m_pRs)
	{
		return m_pRs->IsEof();
	}
	return FALSE;
}
void CSQLiteJsCallImp::NextRow() 
{
	if( NULL != m_pRs)
	{
		m_pRs->NextRow();
	}
}
BOOL CSQLiteJsCallImp::PrepareStatement(LPCWSTR szSQL)
{
	if( NULL != m_pDB && m_pDB->IsOpen())
	{
		if(NULL != m_pSQLStatement)
		{
			m_pSQLStatement->Reset();
			m_pSQLStatement.Release();
		}
		HRESULT hr =  m_pDB->PrepareStatement(szSQL,&m_pSQLStatement);
		if(SUCCEEDED(hr))
			return TRUE;
	}
	return FALSE;
}
int CSQLiteJsCallImp::ExecuteUpdate()
{
	if( NULL != m_pSQLStatement)
	{
		int nRows = 0;
		HRESULT hr = m_pSQLStatement->ExecuteUpdate(&nRows);
		if(SUCCEEDED(hr))
			return nRows;
	}
	return -1;
}
int CSQLiteJsCallImp::GetInt(const int idx)
{
	if( NULL != m_pRs)
		return m_pRs->GetInt(idx);
	return 0;
}
LPCWSTR CSQLiteJsCallImp::GetString(const int idx) 
{
	if( NULL != m_pRs)
		return m_pRs->GetAsString(idx);
	return L"";
}
int CSQLiteJsCallImp::GetInt(LPCWSTR columnName) 
{
	if( NULL != m_pRs)
		return m_pRs->GetInt(columnName);
	return 0;
}
LPCWSTR CSQLiteJsCallImp::GetString(LPCWSTR columnName) 
{
	if( NULL != m_pRs)
		return m_pRs->GetAsString(columnName);
	return L"";
}